package com.wgts.wgts.mvp.view.activity.mycenter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerBindNewPhoneActivityComponent;
import com.wgts.wgts.dagger.module.BindNewPhoneActivityModule;
import com.wgts.wgts.mvp.presenter.IBindNewPhoneActivityPresenter;
import com.wgts.wgts.mvp.presenter.IBindPhoneActivityPersenter;
import com.wgts.wgts.mvp.view.IBindNewPhoneActivityView;
import com.wgts.wgts.netmodle.resp.EditMyCenterMessageEntity;
import com.wgts.wgts.tools.AppInfor;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/9/2911:05
 *
 */public class BindNewPhoneActivity extends BaseActivity implements IBindNewPhoneActivityView{
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.code_et)
    EditText codeEt;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.submit_button)
    RelativeLayout submitButton;

    @Inject
    IBindNewPhoneActivityPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_center_bind_new_phone_activity);
        ButterKnife.bind(this);
        includeTitle.setText("手机号更换");
    }

    @Override
    public void init() {
        DaggerBindNewPhoneActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .bindNewPhoneActivityModule(new BindNewPhoneActivityModule(this))
                .build()
                .inject(this);
    }

    @OnClick({R.id.back_btn_ly, R.id.get_code, R.id.submit_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.get_code:
                sendSms();
                break;
            case R.id.submit_button:
                submit();
                break;
        }
    }

    private void sendSms(){
        if(phone.getText().toString() != null){
            if(phone.getText().toString().length() < 11){
                MoHuToast.showToast("请输入正确的手机号");
                return;
            }
        }else{
            MoHuToast.showToast("请输入正确的手机号");
            return;
        }
        getCode.setEnabled(false);
        showDialog();
        presenter.sendSms(phone.getText().toString().trim(), "UPDATE_PHONE");
        presenter.addCountDownTimeListener();
    }

    private void submit(){
        if(phone.getText().toString() != null){
            if(phone.getText().toString().length() != 11){
                MoHuToast.showToast("请输入正确的手机号");
                return;
            }
        }else{
            MoHuToast.showToast("请输入正确的手机号");
            return;
        }
        if(codeEt.getText().toString() != null){
            if(codeEt.getText().toString().length() < 4){
                MoHuToast.showToast("请输入正确的验证码");
                return;
            }
        }else{
            MoHuToast.showToast("请输入正确的验证码");
            return;
        }
        showDialog();
        presenter.BindNewPhone("UPDATE_USER_PHONE", phone.getText().toString().trim(),
                codeEt.getText().toString().trim());
    }

    @Override
    public void onError(int status, String msg) {
        dialogDismiss();
        MoHuToast.showToast(msg);
    }

    @Override
    public void BindNewPhone(EditMyCenterMessageEntity entity) {
        dialogDismiss();
        AppInfor.getInstance().setUserPhone(entity.userPhone);
        Intent intent = new Intent(BindNewPhoneActivity.this, MyCenterMessageActivity.class);
        MoHuToast.showToast("操作成功");
        startActivity(intent);
        finish();
    }

    @Override
    public void sendSmsResult() {
        dialogDismiss();
        MoHuToast.showToast("发送成功");
    }

    @Override
    public void countDownTimeOver() {
        getCode.setEnabled(true);
        getCode.setText("重新获取");
        presenter.removeCountDownTimeListener();
    }

    @Override
    public void countDowmTime(final Integer integer) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getCode.setText(String.valueOf(integer) + "s");
            }
        });
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        presenter.removeCountDownTimeListener();
    }
}
